package com.zg.shop.cart;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.zg.shop.product.Product;
import com.zg.shop.product.ProductService;

public class CartAction extends ActionSupport{

	private Integer pid;
	private Integer count;
	private ProductService productService;
	
	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public Cart getCart(HttpServletRequest request){
		Cart cart = (Cart) request.getSession().getAttribute("cart");
		if(cart == null){
			cart = new Cart();
			request.getSession().setAttribute("cart", cart);
		}
		return cart;
	}
	public String addCart(){
		Product product = productService.findProductInfoByPid(pid);
		CartItem cartItem = new CartItem();
		cartItem.setCount(count);
		cartItem.setProduct(product);
		HttpServletRequest request = ServletActionContext.getRequest();
		Cart cart = getCart(request);
		
		cart.addCart(cartItem);
		
		return "addCartSuccess";
		
	}
	
	public String clearCart(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Cart cart = getCart(request);
		cart.clearCart();
		return "clearCartSuccess";
	}
	
	public String removeCart(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Cart cart = getCart(request);
		cart.removeCart(pid);
		return "removeCartSuccess";
	}
	
	public String MyCart(){
		return "myCartSuccess";
	}
}
